package com.woniuxy.joylifeforum.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.common.entity.CenterUser;
import com.woniuxy.common.utils.JWTUtils;
import com.woniuxy.joylifeforum.service.CommunityService;
import com.woniuxy.joylifeforum.utils.ForumRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 单个社区页面
 */
@RestController
public class ComDetailController {
    @Autowired
    private CommunityService communityService;
    @Autowired
    private ForumRedis forumRedis;
    /*
    获取社区简介以及关注状态
     */
    @RequestMapping("/comIntroduce")
    public Object comIntroduce(HttpServletRequest request,String forumId) {
        //获取用户id
        //String userId = "111";
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        //查询
        Map<String, Object> synopsis = communityService.findSynopsis(forumId, userId);
        Map<String,Object>result=new HashMap<>();
        result.put("code",200);
        result.put("mes","OK");
        result.put("data",synopsis);
        return result;
    }
    /*
       用户添加，取消关注，同时获取社区及经验数据
        */
    @RequestMapping("/concernsDetail")
    public Object concernsDetail(HttpServletRequest request,String forumId, Integer status) {
        //获取用户id
        //String userId = "111";
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        Map<String,Object> resule=new HashMap<>();
        try {
            if(status==1){
                //关注社区
                communityService.addConcern(userId, forumId);
                //获取社区经验
                Integer exp = communityService.findExp(userId, forumId);
                resule.put("status",1);
                resule.put("exp",exp);
                return resule;
            }else{
                //取消关注
                communityService.cancelConcern(userId, forumId);
                resule.put("status",0);
                //社区关注量
                resule.put("concernNum",forumRedis.findForumUserNum(forumId));
                //社区话题量
                resule.put("topicsNum",forumRedis.findForumTopicNum(forumId));
                return resule;
            }
        } catch (Exception e) {
            e.printStackTrace();
            //异常处理
            resule.put("status",2);
            return resule;
        }
    }
    /*
    获取社区动态 最新-最热-最多收藏
     */
    @RequestMapping("/comNewMes")
    public Object comNewMes(HttpServletRequest request,Integer site,Integer page, Integer limit,String forumId,String find) {
        //获取用户id
        //String userId = "111";
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        Map<String,Object> result=new HashMap<>();
        PageInfo<Map<String, Object>> newMes = communityService.findNewMes(userId,page, limit, forumId, find,site);
        result.put("data",newMes);
        result.put("code",200);
        result.put("mes","OK");
        return result;
    }

    /*
    用户上传照片
     */
    @RequestMapping("/addImg")
    public Object addImg(MultipartFile img) {
        String imgName = communityService.addImgService(img);
        HashMap<String, Object> map = new HashMap<>();
        map.put("url",imgName);
        return map;
    }
    /*
    用户添加话题
     */
    @RequestMapping("/addTopic")
    public Object addTopic(HttpServletRequest request,HttpServletRequest req, String content, String forumId) {
        //图片
        String[] imgs = req.getParameterValues("imgs[]");
        //获取用户id
        //String userId = "111";
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        //发布社区判断
        if("myForum".equals(forumId)){
            //获取用户社区
            CenterUser user = communityService.findUser(userId);
            if(user.getForumId()==null||"".equals(user.getForumId())){
                //未绑定社区
                return 3;
            }
            forumId=user.getForumId();
        }
        String status = communityService.addTopicService(imgs, content, forumId, userId);
        return status;
    }

}
